package com.crm.service.impl;

import com.crm.service.IProcessService;
import com.crm.service.ISloggerService;
import com.crm.dao.IProcessDao;
import com.crm.model.Process;
import com.crm.model.User;
import com.crm.model.SysLog;

import java.util.List;


/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class ProcessService implements IProcessService {

    private static final String INF_CRE = "Create Process";
    private static final String INF_DEL = "Delete Process";
    private static final String INF_UPD = "Update Precess";
    private IProcessDao processDao;
    private ISloggerService slogServ;

    public void setProcessDao(IProcessDao processDao) {
        this.processDao = processDao;
    }

    public void setSlogServ(SloggerService slogServ) {
        this.slogServ = slogServ;
    }    

    /**
     * {@inheritDoc}
     */
    public void create(com.crm.model.Process process) {
        processDao.saveOrUpdate(process);
        slogServ.create(process.getUser(), SysLog.T_PROC, SysLog.ACT_C, INF_CRE);
    }

    /**
     * {@inheritDoc}
     */
    public void update(com.crm.model.Process process) {
        processDao.saveOrUpdate(process);
        slogServ.create(process.getUser(), SysLog.T_PROC, SysLog.ACT_U, INF_UPD);
    }

    /**
     * {@inheritDoc}
     */
    public List getProcesses() {
        return processDao.getProcesses();
    }

    /**
     * {@inheritDoc}
     * todo:修改按联系人信息检索，用户界面暂时不开放。
     */
    public List getProcesses(User user, String[] searchParams) {
        checkUser(user);
        List list;
        String info = "search process info without keywords.";
        if (searchParams[0] != null && searchParams[1].equals("相关客户")) {
            list = getProcesses0(user.getUserGroup(), user.getUserCode(), searchParams[0], "", "");
            info = "search process info by customer. keyword: " + searchParams[0];
        } else if (searchParams[0] != null && searchParams[1].equals("相关联系人")) {
            list = getProcesses0(user.getUserGroup(), user.getUserCode(), "", searchParams[0], "");
            info = "search process info by contact, keyword: " + searchParams[0];
        } else if (searchParams[0] != null && searchParams[1].equals("沟通时间")) {
            list = getProcesses0(user.getUserGroup(), user.getUserCode(), "", "", searchParams[0]);
            info = "search process info by commune date, keyword: " + searchParams[0];
        } else {
            list = getProcesses0(user.getUserGroup(), user.getUserCode(), "", "", "");
        }
        slogServ.create(user, SysLog.T_PROC, SysLog.ACT_R, info);
        return list;
    }

    private List getProcesses0(String ug, String uc, String cust, String cont, String date) {
        if (User.GROUP_SUPER.equals(ug) || User.GROUP_ADMIN.equals(ug)) {
            return processDao.getProcessesByKeywords(cust, cont, date);
        } else {
            return processDao.getProcessesByKeywords(uc, cust, cont, date);
        }
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessByContactId (String contactId){
        return processDao.getProcessesByContactId(contactId);
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessByUser(User user){
        checkUser(user);
        if (user.getUserGroup().equals(User.GROUP_SUPER) || user.getUserGroup().equals(User.GROUP_ADMIN)) {
            return processDao.getProcesses();
        } else {
            return processDao.getProcessesByUserCode(user.getUserCode());
        }
    }

    /**
     * {@inheritDoc}
     */
    public Process getProcessById(String processId){
        return processDao.getProcessById(processId);
    }

    /**
     * {@inheritDoc}
     */
    public String getNewSerialNum(String contactId){
       String serialNum = processDao.getLastSerialNum(contactId);
        if(serialNum == null){
            return "1";
        }
        int serialNumInt = Integer.parseInt(serialNum) + 1;

        return String.valueOf(serialNumInt);
    }

    public List getRecycleProc(String contactId) {
        return processDao.getRecycleProc(contactId);
    }

    /**
     * {@inheritDoc}
     */
    public List getProcessByNoHandle(User user){
        checkUser(user);
//        String ug = user.getUserGroup();
//        if (User.GROUP_SUPER.equals(ug) || User.GROUP_ADMIN.equals(ug)) {
//            return processDao.getProcessesByNoHandle();
//        } else {
            return processDao.getProcessesByNoHandle(user.getUserCode());
//        }
    }

    /**
     * {@inheritDoc}
     */
    public void delete(Process process) {
        processDao.delete(process);
        slogServ.create(process.getUser(), SysLog.T_PROC, SysLog.ACT_D, INF_DEL);
    }

    private void checkUser(User user) {
        if (user.getUsername() == null || user.getPassword() == null) {
            throw new NullPointerException("User session is null!");
        }
    }

}
